#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2011-11-10
@author: shell.xu
'''
import web
import marshal
from web import form

try: from mgsess import MongoStore
except ImportError: pass
try: from rdssess import RedisStore
except ImportError: pass
from beanstalkc import Connection as beanstalk_conn
from brower import Brower

def redirect_wrap(path):
    ''' 重定向到某个url '''
    class inner(object):
         def GET(self): return web.seeother(path)
	 POST = GET
    return inner

def calltmpl(obj, **kargs):
    ''' 调用某个模板，添加urls '''
    kargs['urls'] = web.config.urls
    return obj(searchList = kargs)

def form2obj(f, ws, init = None):
    ''' 将form中ws所列出的fields转换到dict，一般用于生成mongo对象 '''
    r = dict([(n, getattr(f, n).get_value()) for n in ws])
    if init: r.update(init)
    return r
    
def dictcut(d, s):
    ''' 切割dict中的一部分 '''
    r = {}
    for i in s: r[i] = d[i]
    return r

def btmqsend(*p):
    ''' 向beanstalk服务队列发送一个请求，注意拿不到回应 '''
    return web.config.bt.put(marshal.dumps(p))

def zmqreq(*p):
    ''' 向zmq服务队列发送一个请求，并等待回应 '''
    web.config.zmqs.send(marshal.dumps(p))
    return marshal.loads(web.config.zmqs.recv())
